//
//  UIViewController+NavButton.m
//  INMOTION
//
//  Created by Kyle on 14-4-15.
//  Copyright (c) 2014年 Inmotion. All rights reserved.
//

#import "UIViewController+NavButton.h"

@implementation UIViewController (NavButton)

// 添加文字按钮
- (UIBarButtonItem *)addNavButtonWithTitle:(NSString *)title andSide:(NSInteger)side andSeloctor:(SEL)selector {
    UIBarButtonItem *navButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:selector];
    navButton.tintColor = [UIColor orangeColor];
    if (side == 0) {
        self.navigationItem.leftBarButtonItem = navButton;
    } else
        self.navigationItem.rightBarButtonItem = navButton;
    
    return navButton;
}

// 添加图片按钮
- (UIButton *)addNavButtonWithNormalImage:(NSString *)normalImage andPressedImage:(NSString *)pressedImage andSide:(NSInteger)side andSeloctor:(SEL)selector {
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
    [button setImage:[[UIImage imageNamed:normalImage] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    if (pressedImage) {
        [button setImage:[[UIImage imageNamed:pressedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateHighlighted];
    }
    
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    if (side == 0) {
        self.navigationItem.leftBarButtonItem = menuButton;
    } else {
        self.navigationItem.rightBarButtonItem = menuButton;
    }
    
    return button;
}

// 删除图片按钮
- (void)removeNavButton {
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
}

// 判断右上角图片按钮是否存在
- (BOOL)hasNavRightButton {
    if (self.navigationItem.rightBarButtonItem == nil) {
        return NO;
    }
    return YES;
}


- (UIButton *)addBackButton {
    NSString *imageName = @"nav_back_white";
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [backButton addTarget:self action:NSSelectorFromString(@"backButtonPressed:") forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"    " forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -15;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarButton];
    
    return backButton;
}

@end
